package com.yuerenjf.yuerenjinfu.fragment;

/**
 * Created by Administrator on 2017/6/13 0013.
 */

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.yuerenjf.yuerenjinfu.R;
import com.yuerenjf.yuerenjinfu.activity.LoginActivity;
import com.yuerenjf.yuerenjinfu.activity.MainActivity;
import com.yuerenjf.yuerenjinfu.utils.CacheUtils;
import com.yuerenjf.yuerenjinfu.utils.ConstantUtils;

import org.xutils.common.util.LogUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * User: special
 * Date: 13-12-22
 * Time: 下午1:33
 * Mail: specialcyci@gmail.com
 */
public class MainTabFragment extends Fragment {
    private LayoutInflater inflater;
    private View parentView;
    private static List<View> tabList;
    FragmentTabHost tabHost;
    FragmentLendList tab2Fragment;

    public FragmentLendList getTab2Fragment() {
        return tab2Fragment;
    }

    public void setTab2Fragment(FragmentLendList tab2Fragment) {
        this.tab2Fragment = tab2Fragment;
    }

    private Class[] fragmentArray = new Class[]{FragmentHome.class, FragmentLendList.class, FragmentFind.class, FragmentOwn.class};

    private static final String[] tabTitles = new String[]{"首页", "出借", "发现", "我的"};

    private int[] imgRes = new int[]{R.drawable.tab_home_btn, R.drawable.tab_haitao_btn, R.drawable.tab_find_btn, R.drawable.tab_user_btn};

    public static MainTabFragment getInstance(int position){
        MainTabFragment mainTabFragment=new MainTabFragment();
        Bundle bundle=new Bundle();
        bundle.putInt("position",position);
        mainTabFragment.setArguments(bundle);
        return  mainTabFragment;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtil.i("MainTabFragment:onCreate");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        parentView = inflater.inflate(R.layout.fragment_main_tab, container, false);
        LogUtil.i("MainTabFragment:onCreateView");
        tabHost = (FragmentTabHost) parentView.findViewById(R.id.tab_host);
        return parentView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        setUpViews();
    }

    private void setUpViews() {
        inflater = LayoutInflater.from(getActivity());
        tabList = getTabViewList(tabTitles.length);
        initiTabHost();
//        initFragment();
    }

    private List<View> getTabViewList(int len) {
        List<View> list = new ArrayList<>();
        if (len <= 0) return list;
        for (int i = 0; i < len; i++) {
            list.add(getTabItemView(i));
        }
        return list;
    }

    private View getTabItemView(int tabIndex) {
        View view = inflater.inflate(R.layout.item_tab, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.image);
        TextView textView = (TextView) view.findViewById(R.id.textview);
        imageView.setImageResource(imgRes[tabIndex]);
        textView.setText(tabTitles[tabIndex]);
        return view;
    }

    private void initiTabHost() {
        int tab =getArguments().getInt("position");

        Log.i("tab",tab+"");

        tabHost.setup(getActivity(), getActivity().getSupportFragmentManager(), R.id.content);
        tabHost.getTabWidget().setDividerDrawable(null);
//        FragmentHome tab1Fragment = new FragmentHome();
//        tab2Fragment = new FragmentHaitao();
//        FragmentFind tab3Fragment = new FragmentFind();
//        FragmentOwn tab4Fragment = new FragmentOwn();
//        TabHost.TabSpec spec1 = tabHost.newTabSpec(tabTitles[0]).setIndicator(tabList.get(0));
//        TabHost.TabSpec spec2 = tabHost.newTabSpec(tabTitles[1]).setIndicator(tabList.get(1));
//        TabHost.TabSpec spec3 = tabHost.newTabSpec(tabTitles[2]).setIndicator(tabList.get(2));
//        TabHost.TabSpec spec4 = tabHost.newTabSpec(tabTitles[3]).setIndicator(tabList.get(3));
//        tabHost.addTab(spec1,tab1Fragment.getClass(), null);
//        tabHost.addTab(spec2, tab2Fragment.getClass(), null);
//        tabHost.addTab(spec3, tab3Fragment.getClass(), null);
//        tabHost.addTab(spec4, tab4Fragment.getClass(), null);
        for (int i = 0; i < fragmentArray.length; i++) {
            //为每一个Tab按钮设置图标、文字和内容
            TabHost.TabSpec tabSpec = tabHost.newTabSpec(tabTitles[i]).setIndicator(tabList.get(i));
            //将Tab按钮添加进Tab选项卡中
            tabHost.addTab(tabSpec, fragmentArray[i], null);
        }

        tabHost.getTabWidget().getChildTabViewAt(3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtil.i(CacheUtils.getString(getActivity(), ConstantUtils.SP_SECRET));

                if (CacheUtils.isNeedLogin(getActivity())) {
                    LoginActivity.launchLoginActivity(getActivity(), LoginActivity.FROM_TYPE_MAIN_TAB_OWN);
                } else {
                    setTabSelected(3);
                }
            }
        });
        tabHost.setCurrentTab(2);

        tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                int selectedPosition = getTabSelected();
                MainActivity mainActivity = (MainActivity) getActivity();
                if (selectedPosition == 0) {
                    mainActivity.mMenu_right_main_layout.setBackgroundColor(getResources().getColor(R.color.app_theme_color));
                    mainActivity.mMenu_right_list_layout.setBackground(null);
                    mainActivity.mMenu_right_main_text.setTextColor(getResources().getColor(R.color.white));
                    mainActivity.mMenu_right_list_text.setTextColor(Color.parseColor("#adabac"));


                } else if (selectedPosition == 1) {
                    mainActivity.mMenu_right_list_layout.setBackgroundColor(getResources().getColor(R.color.app_theme_color));

                    mainActivity.mMenu_right_main_layout.setBackground(null);
                    mainActivity.mMenu_right_list_text.setTextColor(getResources().getColor(R.color.white));
                    mainActivity.mMenu_right_main_text.setTextColor(Color.parseColor("#adabac"));
                } else {
                    mainActivity.mMenu_right_list_layout.setBackground(null);
                    mainActivity.mMenu_right_main_layout.setBackground(null);
                    mainActivity.mMenu_right_main_text.setTextColor(Color.parseColor("#adabac"));
                    mainActivity.mMenu_right_list_text.setTextColor(Color.parseColor("#adabac"));
                }
            }
        });
        tabHost.setCurrentTab(tab);
    }

    public void setTabSelected(int position) {
        tabHost.setCurrentTab(position);
    }
    public void setTabSelected2(int position) {
    }
    public int getTabSelected() {
        return tabHost.getCurrentTab();
    }

    /**
     * 初始化Fragment
     */
//    private void initFragment() {
//        //通过FragmentManager把两个Fragment添加到Activity中
//        //不能用getFragmentManager();
//        FragmentManager manager = getActivity().getSupportFragmentManager();
//        FragmentTransaction ft = manager.beginTransaction();
//        ft.replace(R.id.content, new FragmentHome());
//        ft.commit();
//    }
}